import { Modal } from "antd";
import Axios, { AxiosResponse, AxiosRequestConfig, Method } from "axios";
import JSONP from "jsonp";

import APIkey from "../config/APIkey";
import { StandardDatas, ResponseResult } from "../utils/interface";

interface JSONPConfig {
	url: string;
	options?: object;
	cb?: Function;
}

interface axiosConfig {
	url: string;
	options?: object;
	cb?: Function;
}

interface RES {
	status: string | number;
}

interface commonAPIConfig {
	url: string;
	method?: Method;
	baseURL?: string;
	timeout?: number;
	data?: Object | undefined;
}

export interface AJAXResponseConfig extends AxiosResponse {}

export interface AJAXRequestConfig extends AxiosRequestConfig {}

export default class AJAX {
	static getLocation() {
		let location = "武汉";
		let url = `https://restapi.amap.com/v3/config/district?keywords=${location}&key=${APIkey.serverKey}`;
		return new Promise((resolve: (value?: any) => void, reject: (reason?: any) => void) => {
			JSONP(url, { param: "callback" }, (err: any, res: any) => {
				if (res.status == 1) {
					resolve(res.districts[0]);
				} else {
					reject("获取位置失败");
				}
			});
		});
	}

	static getWeather() {
		return new Promise((resolve: (value?: any) => void, reject: (reason?: any) => void) => {
			this.getLocation().then(response => {
				let url = `https://restapi.amap.com/v3/weather/weatherInfo?city=${response.adcode}&key=${APIkey.serverKey}`;
				JSONP(url, { param: "callback" }, (err: any, res: any) => {
					if (res.status == 1) {
						resolve(res.lives[0]);
					} else {
						reject("获取天气失败");
					}
				});
			});
		});
	}

	// static getWeather() {
	// 	this.getLocation().then((response)=>{
	// 		let url = `https://restapi.amap.com/v3/weather/weatherInfo?city=${response.adcode}&key=${APIkey.serverKey}`
	// 		return new Promise((resolve: (value?: unknown) => void, reject: (reason?: any) => void) => {
	// 			JSONP(url, { param: "callback" }, (err: any, res: any) => {
	// 				if(res.status == 1){
	// 					resolve(res.lives)
	// 				}else{
	// 					reject('获取天气失败')
	// 				}
	// 			});
	// 		});
	// 	})

	// }

	static jsonp(jsonpConfig: JSONPConfig) {
		return new Promise((resolve: (value?: unknown) => void, reject: (reason?: any) => void) => {
			JSONP(jsonpConfig.url, { param: "callback" }, (err: any, res: RES) => {
				// if(res.status){

				// }
				console.log(res);
			});
		});
	}

	// axiosConfig:axiosConfig

	static get() {
		// Axios.post("www.juhe.cn/box/newtest", { cityname: "武汉", dtype: "json", format: "1" } )
		// 	.then(response => {
		// 		console.log(response);
		// 	})
		// 	.catch(err => {
		// 		console.log(err);
		// 	});
		return new Promise((resolve: (value?: unknown) => void, reject: (reason?: any) => void) => {
			Axios.post("www.juhe.cn/box/newtest", { cityname: "武汉", dtype: "json", format: "1" })
				.then(response => {
					debugger;
					resolve(response);
				})
				.catch(err => {
					debugger;
					reject(err);
				});
		});
	}

	static commonAPI(options: commonAPIConfig) {
		return new Promise((resolve: (value?: any) => void, reject: (reason?: any) => void) => {
			Axios({
				url: options.url,
				method: options.method || "get",
				baseURL: options.baseURL || "",
				timeout: options.timeout || 5000,
				params: options.data || ""
			}).then((response: AxiosResponse) => {
				if (response.status == 200) {
					let res = response.data;
					if (res.code == 0) {
						resolve(res.result);
					} else {
						Modal.info({
							title: "提示",
							content: res.msg || "接口错误"
						});
					}
				} else {
					reject(response.data);
				}
			});
		});
	}

	static standardDataAPI<T>(options: AxiosRequestConfig) {
		return new Promise((resolve: (value?: ResponseResult<T>) => void, reject: (reason?: any) => void) => {
			Axios({
				url: options.url,
				method: options.method || "get",
				baseURL: options.baseURL || "",
				timeout: options.timeout || 5000,
				params: options.params || ""
			}).then((response: AxiosResponse<StandardDatas<T>>) => {
				if (response.status == 200) {
					let res = response.data;
					if (res.code == 0) {
						resolve(res.result);
					} else {
						Modal.info({
							title: "提示",
							content: res.message || "接口错误"
						});
					}
				} else {
					reject(response.data);
				}
			});
		});
	}

	static DBInsert(options: AxiosRequestConfig) {
		return new Promise((resolve: (value: any) => void, reject: (reason: any) => void) => {
			Axios({
				url: options.url,
				method: options.method || "post",
				baseURL: options.baseURL || "http://localhost:8080",
				timeout: options.timeout || 5000,
				params: options.params || ""
			}).then((response: AxiosResponse) => {
				if (response.status == 200) {
					let res = response.data;
					if (res.code == 0) {
						resolve(res.result);
					} else {
						Modal.info({
							title: "提示",
							content: res.message || "接口错误"
						});
					}
				} else {
					reject(response.data);
				}
			});
		});
	}
}
